package com.service.impl;

import com.dao.*;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.*;
import com.util.ExampleBuilder;
import com.util.PageData;
import com.util.PageWrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @ClassName BanciServiceImpl
 * @Description 班次模块业务实现层
 */
@Service
public class BanciServiceImpl implements BanciService {

    @Autowired
    private BanciMapper banciMapper;

    @Autowired
    private LeibieMapper leibieMapper;


    //保存或更新班次表banci
    @Override
    public Integer saveOrUpdate(Banci instance) {
        if(null != instance.getId() && instance.getId() > 0) {
            banciMapper.updateByPrimaryKeySelective(instance);
        }else {
            banciMapper.insertSelective(instance);
        }
        return instance.getId();
    }

    //查询班次表banci
    @Override
    public List<Banci> findAll() {
        List<Banci> list = banciMapper.selectByExample(null);
        for (Banci info:list) {
            setFatherInfo(info);
        }
        return list;
    }

    //根据条件(字符类型模糊匹配)查询班次表banci
    @Override
    public List<Banci> find(Banci instance) {
        BanciExample banciExample = getBanciExampleByLike(instance);
        banciExample.setOrderByClause(" id desc");
        List<Banci> list = banciMapper.selectByExample(banciExample);
        for (Banci info:list) {
            setFatherInfo(info);
        }
        return list;
     }

    //根据条件(字符类型完全匹配)查询班次表banci
    @Override
    public List<Banci> findByEqualTo(Banci instance) {
        BanciExample banciExample = getBanciExampleByEqualTo(instance);
        banciExample.setOrderByClause(" id desc");
        List<Banci> list = banciMapper.selectByExample(banciExample);
        for (Banci info:list) {
            setFatherInfo(info);
        }
        return list;
    }


    //根据条件(字符类型模糊匹配)查询班次表banci最后一条数据
    @Override
    public Banci findOne(Banci instance) {
        List<Banci> list = find(instance);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //根据条件(字符类型完全匹配)查询班次表banci最后一条数据
    @Override
    public Banci findOneByEqualTo(Banci instance) {
        List<Banci> list = findByEqualTo(instance);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //根据id列查询班次表banci数据
    @Override
    public Banci findById(Integer id) {
        Banci info = banciMapper.selectByPrimaryKey(id);
        setFatherInfo(info);
        return info;
    }

    //根据主键id列删除班次表banci数据
    @Override
    public int deleteById(Integer id) {
        return banciMapper.deleteByPrimaryKey(id);
    }


    //根据条件(字符类型模糊匹配)分页查询班次表banci数据
    @Override
    public PageData<Banci> findPage(PageWrap<Banci> pageWrap) {
        PageHelper.startPage(pageWrap.getPageNum(), pageWrap.getPageSize());
        BanciExample banciExample = getBanciExampleByLike(pageWrap.getModel());
        banciExample.setOrderByClause(pageWrap.getOrderByClause());
        List<Banci> list = banciMapper.selectByExample(banciExample);
        for (Banci info:list) {
            setFatherInfo(info);
        }
        return PageData.from(new PageInfo<>(list));
    }

    //根据条件(字符类型完全匹配)分页查询班次表banci数据
    @Override
    public PageData<Banci> findPageByEqualTo(PageWrap<Banci> pageWrap) {
        PageHelper.startPage(pageWrap.getPageNum(), pageWrap.getPageSize());
        BanciExample banciExample = getBanciExampleByEqualTo(pageWrap.getModel());
        banciExample.setOrderByClause(pageWrap.getOrderByClause());
        List<Banci> list = banciMapper.selectByExample(banciExample);
        for (Banci info:list) {
            setFatherInfo(info);
        }
        return PageData.from(new PageInfo<>(list));
    }

    //父表信息
    private void setFatherInfo(Banci info){
        if (null != info){
            Leibie leibie= leibieMapper.selectByPrimaryKey(info.getLeibieid());
            info.setLeibie(leibie);

        }
    }

    //字符类型模糊查询条件构造器
    private BanciExample getBanciExampleByLike(Banci instance) {
        BanciExample banciExample = new BanciExample();
        BanciExample.Criteria criteria = banciExample.createCriteria();
        //班次id完全匹配
        if (null != instance.getId()) {
            criteria.andIdEqualTo(instance.getId());
        }
        //编号bianhao模糊匹配
        if (null != instance.getBianhao() && !instance.getBianhao().equals("")) {
            criteria.andBianhaoLike("%" + instance.getBianhao() + "%");
        }
        //类别idleibieid完全匹配
        if (null != instance.getLeibieid()) {
            criteria.andLeibieidEqualTo(instance.getLeibieid());
        }
        //起始站qishizhan模糊匹配
        if (null != instance.getQishizhan() && !instance.getQishizhan().equals("")) {
            criteria.andQishizhanLike("%" + instance.getQishizhan() + "%");
        }
        //终点站zhongdianzhan模糊匹配
        if (null != instance.getZhongdianzhan() && !instance.getZhongdianzhan().equals("")) {
            criteria.andZhongdianzhanLike("%" + instance.getZhongdianzhan() + "%");
        }
        //起始时间qishishijian模糊匹配
        if (null != instance.getQishishijian() && !instance.getQishishijian().equals("")) {
            criteria.andQishishijianLike("%" + instance.getQishishijian() + "%");
        }
        //到达时间daodashijian模糊匹配
        if (null != instance.getDaodashijian() && !instance.getDaodashijian().equals("")) {
            criteria.andDaodashijianLike("%" + instance.getDaodashijian() + "%");
        }
        //日期riqi模糊匹配
        if (null != instance.getRiqi() && !instance.getRiqi().equals("")) {
            criteria.andRiqiLike("%" + instance.getRiqi() + "%");
        }
        //价格jiage完全匹配
        if (null != instance.getJiage()) {
            criteria.andJiageEqualTo(instance.getJiage());
        }
        //票数shuliang完全匹配
        if (null != instance.getShuliang()) {
            criteria.andShuliangEqualTo(instance.getShuliang());
        }
        //备注beizhu模糊匹配
        if (null != instance.getBeizhu() && !instance.getBeizhu().equals("")) {
            criteria.andBeizhuLike("%" + instance.getBeizhu() + "%");
        }

        return banciExample;
    }

    //字符类型完全相等查询条件构造器
    private BanciExample getBanciExampleByEqualTo(Banci instance) {
        BanciExample banciExample = new BanciExample();
        BanciExample.Criteria criteria = banciExample.createCriteria();
        //班次id完全匹配
        if (null != instance.getId()) {
            criteria.andIdEqualTo(instance.getId());
        }
        //编号bianhao完全匹配
        if (null != instance.getBianhao() && !instance.getBianhao().equals("")) {
            criteria.andBianhaoEqualTo(instance.getBianhao());
        }
        //类别idleibieid完全匹配
        if (null != instance.getLeibieid()) {
            criteria.andLeibieidEqualTo(instance.getLeibieid());
        }
        //起始站qishizhan完全匹配
        if (null != instance.getQishizhan() && !instance.getQishizhan().equals("")) {
            criteria.andQishizhanEqualTo(instance.getQishizhan());
        }
        //终点站zhongdianzhan完全匹配
        if (null != instance.getZhongdianzhan() && !instance.getZhongdianzhan().equals("")) {
            criteria.andZhongdianzhanEqualTo(instance.getZhongdianzhan());
        }
        //起始时间qishishijian完全匹配
        if (null != instance.getQishishijian() && !instance.getQishishijian().equals("")) {
            criteria.andQishishijianEqualTo(instance.getQishishijian());
        }
        //到达时间daodashijian完全匹配
        if (null != instance.getDaodashijian() && !instance.getDaodashijian().equals("")) {
            criteria.andDaodashijianEqualTo(instance.getDaodashijian());
        }
        //日期riqi完全匹配
        if (null != instance.getRiqi() && !instance.getRiqi().equals("")) {
            criteria.andRiqiEqualTo(instance.getRiqi());
        }
        //价格jiage完全匹配
        if (null != instance.getJiage()) {
            criteria.andJiageEqualTo(instance.getJiage());
        }
        //票数shuliang完全匹配
        if (null != instance.getShuliang()) {
            criteria.andShuliangEqualTo(instance.getShuliang());
        }
        //备注beizhu完全匹配
        if (null != instance.getBeizhu() && !instance.getBeizhu().equals("")) {
            criteria.andBeizhuEqualTo(instance.getBeizhu());
        }

        return banciExample;
    }
}
